package com.example.zhunongzonghe.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhunongzonghe.beans.ZnScenicEvaluateBean;
import com.example.zhunongzonghe.beans.ZnScenicEvaluateLikeBean;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.service.ZnScenicEvaluateLikeService;
import com.example.zhunongzonghe.service.ZnScenicEvaluateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * (ZnScenicEvaluate)表控制层
 *
 * @author makejava
 * @since 2024-04-25 17:02:26
 */
@RestController
@RequestMapping("/znScenicEvaluate")
public class ZnScenicEvaluateController {
    /**
     * 服务对象
     */
    @Resource
    private ZnScenicEvaluateService znScenicEvaluateService;
    @Resource
    private ZnScenicEvaluateLikeService evaluateLikeService;
    /**
     * 用户未登录时查询全部景点评价
     * @param currentPage
     * @param pageSize
     * @param ScenicId
     * @return
     */
    @GetMapping("/selectNoLoginEvaluateAll")
    public Result selectNoLoginEvaluateAll(@RequestParam("currentPage") Integer currentPage,
                                           @RequestParam("pageSize") Integer pageSize,
                                           @RequestParam("ScenicId") Integer ScenicId){
        if (ScenicId==null) return new Result(false,"失败",400);
        Page<ZnScenicEvaluateBean> beanPage =
                znScenicEvaluateService.selectNoLoginEvaluateAll(currentPage,pageSize,ScenicId);
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 用户登录时查询全部评价
     * @param currentPage
     * @param pageSize
     * @param ScenicId
     * @param principal
     * @return
     */
    @GetMapping("/selectEvaluateAll")
    public Result selectEvaluateAll(@RequestParam("currentPage") Integer currentPage,
                                           @RequestParam("pageSize") Integer pageSize,
                                           @RequestParam("ScenicId") Integer ScenicId,Principal principal){
        if (ScenicId==null) return new Result(false,"失败",400);
        Page<ZnScenicEvaluateBean> beanPage =
                znScenicEvaluateService.selectEvaluateAll(currentPage,pageSize,ScenicId, principal.getName());
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 用户点赞评价
     * @param likeBean
     * @param principal
     * @return
     */
    @RequestMapping("/addEvaluateLike")
    public Result addEvaluateLike(@RequestBody ZnScenicEvaluateLikeBean likeBean,Principal principal){
        evaluateLikeService.addLike(likeBean, principal.getName());
        return new Result(true,"成功",200);
    }

    /**
     * 新增评价
     * @param znScenicEvaluateBean
     * @param principal
     * @return
     */
    @RequestMapping("/addEvaluate")
    public Result addEvaluate(@RequestBody ZnScenicEvaluateBean znScenicEvaluateBean, Principal principal){
        znScenicEvaluateService.addEvaluate(znScenicEvaluateBean, principal.getName());
        return new Result(true,"成功",200);
    }

    /**
     *  用户修改评价
     * @param znScenicEvaluateBean
     * @return
     */
    @RequestMapping("/editEvaluate")
    public Result editEvaluate(@RequestBody ZnScenicEvaluateBean znScenicEvaluateBean){
        znScenicEvaluateService.editEvaluate(znScenicEvaluateBean);
        return new Result(true,"成功",200);
    }

    /**
     * 用户删除自己的点评
     * @param id
     * @return
     */
    @DeleteMapping("/delEvaluate/{id}")
    private Result delEvaluate(@PathVariable Integer id){
        if (id==null) return new Result(false,"失败",400);
        znScenicEvaluateService.removeById(id);
        return new Result(true,"成功",200);
    }

    /**
     * 后台查询全部旅游景点点评
     * @param currentPage
     * @param pageSize
     * @param searchInfo
     * @return
     */
    @GetMapping("/getMagEvaluateAll")
    public Result getMagEvaluateAll (@RequestParam("currentPage")Integer currentPage,
                                     @RequestParam("pageSize")Integer pageSize,
                                     @RequestParam("searchInfo")Object searchInfo){
        Page<ZnScenicEvaluateBean> beanPage = znScenicEvaluateService.getMagEvaluateAll(currentPage,pageSize,searchInfo);
        if (currentPage > beanPage.getPages()){
            beanPage=znScenicEvaluateService.getMagEvaluateAll((int) beanPage.getPages(),pageSize,searchInfo);
        }
        return new Result(true,"成功",200,beanPage);
    }

    /**
     * 后台删除旅游景点点评
     * @param id
     * @return
     */
    @DeleteMapping("/DeleteScenicEvaluate/{id}")
    public Result deleteScenicEvaluate(@PathVariable Integer id){
        if (id==null) return new Result(false,"失败",400);
        znScenicEvaluateService.removeById(id);
        return new Result(true,"成功",200);
    }
}

